const path = require('path');
var webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

var config = {
    entry: {
        // ueditor_config: ['./app/lib/ueditor1.4.3.3/ueditor.config.js'],
        // ueditor_all: ['./app/lib/ueditor1.4.3.3/ueditor.all.js'],
        app: './app/index.js',
        vendor: ['angular', 'oclazyload']
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: [{
                        loader: "css-loader"
                    }]
                })
            },
            {
                test: /\.scss$/,
                use: [{
                    loader: "style-loader" // 将 JS 字符串生成为 style 节点
                }, {
                    loader: "css-loader" // 将 CSS 转化成 CommonJS 模块
                }, {
                    loader: "sass-loader" // 将 Sass 编译成 CSS
                }]
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {}
                    }
                ]
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {}
                    }
                ]
            },
            {
                test: /\.html$/,
                loader: 'raw-loader',
                exclude: /node_modules/
            }
        ]
    },

    plugins: [
        new ExtractTextPlugin("styles.css"),
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            title: 'angular',
            template: 'build/index.html',
            hash: true,
            // chunks: ['ueditor_config', 'ueditor_all','app','vendor']
        }),
        new webpack.DefinePlugin({
            ON_DEMO: process.env.NODE_ENV === 'demo'
        }),
        new CopyWebpackPlugin([
            // {
            //     from: path.resolve(__dirname, './app/lib/ueditor1.4.3.3'),
            //     to: path.resolve(__dirname, './dist/lib/ueditor1.4.3.3'),
            //     ignore: ['.*']
            // },
            // {
            //     from: path.resolve(__dirname, './node_modules/ztree'),
            //     to: path.resolve(__dirname, './dist/ztree'),
            //     ignore: ['.*']
            // }
        ])
    ],
    optimization: {
        splitChunks: {
            name: 'vendor',
            filename: 'vendor.bundle.js'
        }
    },
    output: {
        filename: '[name].bundle.js',
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist')
    }
};

module.exports = config;